home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / SCRIB3.PAK / SCRIBDOC.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  6KB  |  234 lines

  1. // ScribDoc.cpp : implementation of the CScribbleDoc class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "Scribble.h"
  15.  
  16. #include "ScribDoc.h"
  17. #include "PenDlg.h"
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CScribbleDoc
  27.  
  28. IMPLEMENT_DYNCREATE(CScribbleDoc, CDocument)
  29.  
  30. BEGIN_MESSAGE_MAP(CScribbleDoc, CDocument)
  31.     //{{AFX_MSG_MAP(CScribbleDoc)
  32.     ON_COMMAND(ID_EDIT_CLEAR_ALL, OnEditClearAll)
  33.     ON_COMMAND(ID_PEN_THICK_OR_THIN, OnPenThickOrThin)
  34.     ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR_ALL, OnUpdateEditClearAll)
  35.     ON_UPDATE_COMMAND_UI(ID_PEN_THICK_OR_THIN, OnUpdatePenThickOrThin)
  36.     ON_COMMAND(ID_PEN_WIDTHS, OnPenWidths)
  37.     //}}AFX_MSG_MAP
  38. END_MESSAGE_MAP()
  39.  
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CScribbleDoc construction/destruction
  42.  
  43. CScribbleDoc::CScribbleDoc()
  44. {
  45.     // TODO: add one-time construction code here
  46.  
  47. }
  48.  
  49. CScribbleDoc::~CScribbleDoc()
  50. {
  51. }
  52.  
  53. BOOL CScribbleDoc::OnNewDocument()
  54. {
  55.     if (!CDocument::OnNewDocument())
  56.         return FALSE;
  57.     InitDocument();
  58.     return TRUE;
  59. }
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CScribbleDoc serialization
  63.  
  64. void CScribbleDoc::Serialize(CArchive& ar)
  65. {
  66.     if (ar.IsStoring())
  67.     {
  68.     }
  69.     else
  70.     {
  71.     }
  72.     m_strokeList.Serialize(ar);
  73. }
  74.  
  75. /////////////////////////////////////////////////////////////////////////////
  76. // CScribbleDoc diagnostics
  77.  
  78. #ifdef _DEBUG
  79. void CScribbleDoc::AssertValid() const
  80. {
  81.     CDocument::AssertValid();
  82. }
  83.  
  84. void CScribbleDoc::Dump(CDumpContext& dc) const
  85. {
  86.     CDocument::Dump(dc);
  87. }
  88. #endif //_DEBUG
  89.  
  90. /////////////////////////////////////////////////////////////////////////////
  91. // CScribbleDoc commands
  92.  
  93. BOOL CScribbleDoc::OnOpenDocument(LPCTSTR lpszPathName) 
  94. {
  95.     if (!CDocument::OnOpenDocument(lpszPathName))
  96.         return FALSE;
  97.     InitDocument(); 
  98.     return TRUE;
  99. }
  100.  
  101. void CScribbleDoc::DeleteContents() 
  102. {
  103.     while (!m_strokeList.IsEmpty())
  104.     {
  105.         delete m_strokeList.RemoveHead();
  106.     }
  107.     CDocument::DeleteContents();
  108. }
  109.  
  110. void CScribbleDoc::InitDocument()
  111. {
  112.     m_bThickPen = FALSE;
  113.     m_nThinWidth = 2;   // default thin pen is 2 pixels wide
  114.     m_nThickWidth = 5;  // default thick pen is 5 pixels wide
  115.     ReplacePen();       // initialize pen according to current width
  116. }
  117.  
  118. CStroke* CScribbleDoc::NewStroke()
  119. {
  120.     CStroke* pStrokeItem = new CStroke(m_nPenWidth);
  121.     m_strokeList.AddTail(pStrokeItem);
  122.     SetModifiedFlag();  // Mark the document as having been modified, for
  123.                         // purposes of confirming File Close.
  124.     return pStrokeItem;
  125. }
  126.  
  127.  
  128.  
  129.  
  130. /////////////////////////////////////////////////////////////////////////////
  131. // CStroke
  132.  
  133. IMPLEMENT_SERIAL(CStroke, CObject, 1)
  134. CStroke::CStroke()
  135. {
  136.     // This empty constructor should be used by serialization only
  137. }
  138.  
  139. CStroke::CStroke(UINT nPenWidth)
  140. {
  141.     m_nPenWidth = nPenWidth;
  142. }
  143.  
  144. void CStroke::Serialize(CArchive& ar)
  145. {
  146.     if (ar.IsStoring())
  147.     {
  148.         ar << (WORD)m_nPenWidth;
  149.         m_pointArray.Serialize(ar);
  150.     }
  151.     else
  152.     {
  153.         WORD w;
  154.         ar >> w;
  155.         m_nPenWidth = w;
  156.         m_pointArray.Serialize(ar);
  157.     }
  158. }
  159.  
  160. BOOL CStroke::DrawStroke(CDC* pDC)
  161. {
  162.     CPen penStroke;
  163.     if (!penStroke.CreatePen(PS_SOLID, m_nPenWidth, RGB(0,0,0)))
  164.         return FALSE;
  165.     CPen* pOldPen = pDC->SelectObject(&penStroke);
  166.     pDC->MoveTo(m_pointArray[0]);
  167.     for (int i=1; i < m_pointArray.GetSize(); i++)
  168.     {
  169.         pDC->LineTo(m_pointArray[i]);
  170.     }
  171.  
  172.     pDC->SelectObject(pOldPen);
  173.     return TRUE;
  174. }
  175. void CScribbleDoc::OnEditClearAll() 
  176. {
  177.     DeleteContents();
  178.     SetModifiedFlag();  // Mark the document as having been modified, for
  179.                         // purposes of confirming File Close.
  180.     UpdateAllViews(NULL);
  181. }
  182.  
  183. void CScribbleDoc::OnPenThickOrThin() 
  184. {
  185.     // Toggle the state of the pen between thin or thick.
  186.     m_bThickPen = !m_bThickPen;
  187.  
  188.     // Change the current pen to reflect the new user-specified width.
  189.     ReplacePen();
  190. }
  191.  
  192. void CScribbleDoc::ReplacePen()
  193. {
  194.     m_nPenWidth = m_bThickPen? m_nThickWidth : m_nThinWidth;
  195.  
  196.     // Change the current pen to reflect the new user-specified width.
  197.     m_penCur.DeleteObject();
  198.     m_penCur.CreatePen(PS_SOLID, m_nPenWidth, RGB(0,0,0)); // solid black
  199. }
  200.  
  201. void CScribbleDoc::OnUpdateEditClearAll(CCmdUI* pCmdUI) 
  202. {
  203.     // Enable the command user interface object (menu item or tool bar
  204.     // button) if the document is non-empty, i.e., has at least one stroke.
  205.     pCmdUI->Enable(!m_strokeList.IsEmpty());
  206. }
  207.  
  208. void CScribbleDoc::OnUpdatePenThickOrThin(CCmdUI* pCmdUI) 
  209. {
  210.     // Add check mark to Draw Thick Line menu item, if the current
  211.     // pen width is "thick".
  212.     pCmdUI->SetCheck(m_bThickPen);
  213. }
  214.  
  215. void CScribbleDoc::OnPenWidths() 
  216. {
  217.     CPenWidthsDlg dlg;
  218.     // Initialize dialog data
  219.     dlg.m_nThinWidth = m_nThinWidth;
  220.     dlg.m_nThickWidth = m_nThickWidth;
  221.  
  222.     // Invoke the dialog box
  223.     if (dlg.DoModal() == IDOK)
  224.     {
  225.         // retrieve the dialog data
  226.         m_nThinWidth = dlg.m_nThinWidth;
  227.         m_nThickWidth = dlg.m_nThickWidth;
  228.  
  229.         // Update the pen that is used by views when drawing new strokes,
  230.         // to reflect the new pen width definitions for "thick" and "thin".
  231.         ReplacePen();
  232.     }
  233. }
  234.